package com.jt.www.job.issue;

import com.jt.www.common.service.JobQueryListService;
import com.jt.www.dao.mapper.issue.mapper.IssuePolicyLogEntityMapper;
import com.jt.www.dao.mapper.issue.mapper.IssueRecordEntityMapper;
import com.jt.www.enums.issue.IssueEnum;
import com.jt.www.model.issue.entity.IssuePolicyLogEntity;
import com.jt.www.model.issue.entity.IssueRecordEntity;
import com.jt.www.util.DateUtils;
import com.jt.www.util.JsonUtils;
import com.jt.www.util.RedisUtil;
import org.apache.commons.lang.time.DateFormatUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.*;

import static com.jt.www.enums.issue.PolicyEnum.ZH_SZ_XZ_POLICY;

/**
 * 电子保单是否消费的定时任务
 */

@Component
@EnableScheduling
public class CheckIssueMQRecordTask implements SchedulingConfigurer {

    public static final Logger log = LoggerFactory.getLogger(CheckIssueMQRecordTask.class);
    /**
     * 单个服务器出单查询缓存失效时间 因不同的定时任务决定
     */
    private static final long ISSUE_MQ_TIME_OUT = 40L;
    /**
     * 检查 电子保单是否消费的定时任务 缓存key
     */
    private static final String ISSUE_MQ_LOCK_KEY = "EDU_ISSUE_MQ";
    @Autowired
    IssuePolicyLogEntityMapper issuePolicyLogEntityMapper;
    @Autowired
    IssueRecordEntityMapper issueRecordEntityMapper;
    @Autowired
    JobQueryListService jobQueryListService;
    /**
     * 任务开关
     */
    @Value("${jtpf.issuemq.open}")
    private int checkOpen;
    /**
     * 任务执行周期
     */
    @Value("${jtpf.issuemq.cron}")
    private String cron;// 每隔一分钟执行一次

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        if (checkOpen != 1) {
            return;
        }
        if (RedisUtil.setLock(ISSUE_MQ_LOCK_KEY, "1", ISSUE_MQ_TIME_OUT)) {
            Runnable task0 = () -> checkStart();
            Trigger trigger0 = triggerContext -> new CronTrigger(cron).nextExecutionTime(triggerContext);
            taskRegistrar.addTriggerTask(task0, trigger0);
        }
    }

    private void checkStart() {
        String currentTime = DateUtils.getCurrentTime();
        log.info(currentTime + "CheckAgreeExpireTask satrt,检查出单电子保单是否消费的任务开始...");

        Weekend<IssueRecordEntity> weekend = Weekend.of(IssueRecordEntity.class);
        WeekendCriteria<IssueRecordEntity, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(IssueRecordEntity::getSendStatus, "1");//1:未处理
        criteria.andLessThan(IssueRecordEntity::getStatus, "2");//只有成功的处理

        List<IssueRecordEntity> resultList = jobQueryListService.getJobProcessIssueRecordList(weekend);

        if (resultList != null && resultList.size() > 0) {

            for (IssueRecordEntity t : resultList) {

                String ifCode = t.getIfCode();

                switch (ifCode) {
                    case "20190528001":
                    case "20190528002":
                    case "20190528003":
                        //华泰产品（泰然无忧百万医疗） or //华泰产品（华泰财险成长护航重疾险） or //华泰产品（华泰财险安康保护伞）
                        handleHT(t);
                        break;
                    case "20190516002":
                        //山东产品（山东省校（园）方责任保险项目）
                        handleSD(t);
                        break;
                    case "20190516006":
                        //云南非九（校方综合防范保障保险、教职员工校方责任保险、云南省校方责任保险、校园食品安全责任保险）
                        handleYNFJ(t);
                        break;
                    case "20190515002":
                    case "20190726012":
                        //1实责（全国职业院校学生实习责任保险）  2校责（校方责任保险）
                        handleXZANDSZ(t);
                        break;
                    case "20190515026":
                        //学责（学生安全责任保险）
                        handleXZ(t);
                        break;
                    case "20190515006":
                    case "202008191339340":
                        //阳光财产安责保护伞
                        handleAZBHS(t);
                        break;
                    case "20190802002":
                        //食品险（校园食品安全责任保险）
                        handleSPX(t);
                        break;
                    case "20190729001":
                    case "202008181443590"://徐州学平险（徐州学平意外险/徐州监护人责任险）
//                    case "202008201400250"://临沂学平险与徐州方式相同
                        handleXZXPX(t);
                        break;
                    case "20190805001": //济宁学平险
                    case "20190807001": //校园食品
                        handleEpolicy(t, IssueEnum.getIssueEnum(ifCode));
                        break;
                    case "20190518001":
                        //实责校责（大地）
                        handleSZXZDD(t);
                        break;
                    case "20190726017": //实责校责（太平）
                    case "20190731001":   //实责校责（中华联合）
                        handleSZXZ(t);
                        break;
                    case "20190802001":
                        //食品险（太平）
                        handleSPXTP(t);
                        break;
                    /*case "20190726012":
                        //实责校责（阳光）
                        handleXSZAndXZ(t);
                        break;*/
                    case "20190805002":
                        //食品险（阳光）
                        //handleSPXSP(t);
                        break;
                    case "202008041342390":
                    case "202008180929030":
                    case "202008190924410":
                    case "202008190922090":
                        handleGD(t);
                        break;
                    case "202008241457160":
                        handleJiNingXPX(t);
                        break;
                    case "202009090116140":
                        handleJiNingXPXOnline(t);
                        break;
                    case "202009171037520":
                        handleQiDong(t);
                        break;
                    default:
                        log.error("错误的ifCode {}", ifCode);
                }
            }

        }
        log.info(currentTime + "CheckMessageConsumeFailTask end,检查出单电子保单是否消费的任务结束...");
    }

    /**
     * 华泰产品
     *
     * @param t
     */
    private void handleHT(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费  ： 需要组织报文，去调用接口
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());
        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");
        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());

        String respPacket = t.getResPacket();
        Element rootElement = null;
        try {
            Document doc = DocumentHelper.parseText(respPacket);
            rootElement = doc.getRootElement();

        } catch (DocumentException e) {
            e.printStackTrace();
        }

        String policyURL = rootElement.element("policyURL").getStringValue();

        // 组装报文
        Document doc = DocumentHelper.createDocument();
        Element info = doc.addElement("body");
        info.addElement("url").addText(policyURL);
        issuePolicyLogEntity.setParam(info.asXML());
        issuePolicyLogEntity.setMainType(t.getMainType());
        issuePolicyLogEntity.setTimes(0);
        issuePolicyLogEntity.setIfCode(t.getIfCode());
        issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
        // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
        t.setSendStatus("2");
        issueRecordEntityMapper.updateByPrimaryKeySelective(t);

    }

    /**
     * 1实责（全国职业院校学生实习责任保险）  2校责（校方责任保险）
     *
     * @param t
     */
    private void handleXZANDSZ(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费  ： 需要组织报文，去调用接口
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());
        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");
        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        // 组装报文
        Document doc = DocumentHelper.createDocument();
        Element info = doc.addElement("POLICYNO");
        info.addText(t.getPolicyNo());
        issuePolicyLogEntity.setParam(info.asXML());
        issuePolicyLogEntity.setMainType(t.getMainType());
        issuePolicyLogEntity.setTimes(0);
        issuePolicyLogEntity.setIfCode(t.getIfCode());
        issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
        // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
        t.setSendStatus("2");
        issueRecordEntityMapper.updateByPrimaryKeySelective(t);

    }

    /**
     * 学责（学生安全责任保险）
     *
     * @param t
     */
    private void handleXZ(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费  ： 需要组织报文，去调用接口
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());
        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");
        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        // 组装报文
        Document doc = DocumentHelper.createDocument();
        Element info = doc.addElement("Message");
        doc.setRootElement(info);
        Element body = info.addElement("Body");
        Element policies = body.addElement("Policies");
        policies.addElement("ApplicationFormCode").addText(t.getApplicationFormCode());
        policies.addElement("PolicyNo").addText(t.getPolicyNo());
        policies.addElement("CompanyCode").addText(t.getCompanyCode());
        issuePolicyLogEntity.setParam(info.asXML());
        issuePolicyLogEntity.setMainType(t.getMainType());
        issuePolicyLogEntity.setTimes(0);
        issuePolicyLogEntity.setIfCode(t.getIfCode());
        issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
        // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
        t.setSendStatus("2");
        issueRecordEntityMapper.updateByPrimaryKeySelective(t);
    }

    /**
     * 云南非九（校方综合防范保障保险、教职员工校方责任保险、云南省校方责任保险、校园食品安全责任保险）
     *
     * @param t
     */
    private void handleYNFJ(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费:从响应体中解析出 url，去调用数据中心的接口，下载电子保单
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";//未处理
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());

        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");//电子保单

        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        String resPacket = t.getResPacket();
        String url = "";
        try {
            Document doc = DocumentHelper.parseText(resPacket);
            Element rootElement = doc.getRootElement();
            url = rootElement.element("Body").element("Apply").element("PolicyImage").getStringValue();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        // 拼装下载报文
        Document doc = DocumentHelper.createDocument();
        Element info = doc.addElement("Body");
        info.addElement("url").addText(url);
        issuePolicyLogEntity.setParam(info.asXML());
        issuePolicyLogEntity.setTimes(0);
        issuePolicyLogEntity.setIfCode(t.getIfCode());
        issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
        // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
        t.setSendStatus("2");
        issueRecordEntityMapper.updateByPrimaryKeySelective(t);
    }

    /**
     * 山东产品（山东省校（园）方责任保险项目）
     *
     * @param t
     */
    private void handleSD(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费:从响应体中解析出 url，去调用数据中心的接口，下载电子保单
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";//未处理
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());

        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");//电子保单

        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        String resPacket = t.getResPacket();
        log.info("检查mq消费记录的山东校责请求体是" + resPacket);
        String url = "";
        try {
            Document doc = DocumentHelper.parseText(resPacket);
            Element rootElement = doc.getRootElement();
            Element element = rootElement.element("body").element("apply");
            // 排除 批单的信息
            if (element != null) {
                url = element.element("electricalDocument").getStringValue();
                // 拼装下载报文
                doc = DocumentHelper.createDocument();
                Element info = doc.addElement("body");
                info.addElement("url").addText(url);
                issuePolicyLogEntity.setParam(info.asXML());
                issuePolicyLogEntity.setTimes(0);
                issuePolicyLogEntity.setIfCode(t.getIfCode());
                issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
                // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
                t.setSendStatus("2");
                issueRecordEntityMapper.updateByPrimaryKeySelective(t);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 阳光财产安责保护伞
     *
     * @param t
     */
    private void handleAZBHS(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费:从响应体中解析出 url，去调用数据中心的接口，下载电子保单
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";//未处理
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());

        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");//电子保单

        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        String resPacket = t.getResPacket();
        log.info("检查mq消费记录的阳光财产安责保护伞请求体是" + resPacket);
        try {
            Document doc = DocumentHelper.parseText(resPacket);
            Element rootElement = doc.getRootElement();
            Element element = rootElement.element("Body").element("Apply");
            // 排除无返回结果的报文
            if (element != null) {
                // 拼装下载报文
                doc = DocumentHelper.createDocument();
                Element info = doc.addElement("data");
                info.addElement("policyNo").addText(t.getPolicyNo());
                issuePolicyLogEntity.setParam(info.asXML());
                issuePolicyLogEntity.setTimes(0);
                issuePolicyLogEntity.setIfCode(t.getIfCode());
                issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
                // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
                t.setSendStatus("2");
                issueRecordEntityMapper.updateByPrimaryKeySelective(t);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 食品险（校园食品安全责任保险）
     *
     * @param t
     */
    private void handleSPX(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费:从响应体中解析出 url，去调用数据中心的接口，下载电子保单
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";//未处理
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());

        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");//电子保单

        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        String resPacket = t.getResPacket();
        log.info("检查mq消费记录的食品险请求体是" + resPacket);

        try {
            Document doc = DocumentHelper.parseText(resPacket);
            Element rootElement = doc.getRootElement();
            Element element = rootElement.element("Body").element("Apply");
            // 排除无返回结果的报文
            if (element != null) {
                String policyUrl = element.element("PolicyUrl").getStringValue();
                // 拼装下载报文
                Map<String, String> map = new HashMap<>();
                map.put("ApplicationFormCode", t.getApplicationFormCode());
                map.put("url", policyUrl);

                String param = JsonUtils.toJson(map);

                issuePolicyLogEntity.setParam(param);
                issuePolicyLogEntity.setTimes(0);
                issuePolicyLogEntity.setIfCode(t.getIfCode());
                issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
                // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
                t.setSendStatus("2");
                issueRecordEntityMapper.updateByPrimaryKeySelective(t);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 徐州学平险（徐州学平意外险/徐州监护人责任险）
     *
     * @param t
     */
    private void handleXZXPX(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费:从响应体中解析出 url，去调用数据中心的接口，下载电子保单
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";//未处理
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());

        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");//电子保单

        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        String resPacket = t.getResPacket();
        String reqPacket = t.getReqPacket();
        log.info("检查mq消费记录的徐州学平险请求体是" + resPacket);
        try {

            Document doc = DocumentHelper.parseText(resPacket);
            Element rootElement = doc.getRootElement();
            Element element = rootElement.element("body").element("PolicyApplyResponse");
            // 排除无返回结果的报文
            if (element != null) {
                Document reqPacketDoc = DocumentHelper.parseText(reqPacket);
                Element reqRootElement = reqPacketDoc.getRootElement();
                // 108001 代表徐州学平意外险， 107001 代表徐州监护人责任险
                String transactionCode = reqRootElement.element("head").element("transactionCode").getStringValue();

                //流水号
                String seqNo = Long.toString(Math.abs(new Random().nextLong()));

                // 拼装下载报文
                Document paramDoc = DocumentHelper.createDocument();
                Element info = paramDoc.addElement("request");
                Element head = info.addElement("head");
                //转换编码
                //transactionCode = 108001(代表徐州学平意外险) 调用数据交换平台下载电子保单时传输编码为：108005
                //transactionCode = 107001(代表徐州学平意外险) 调用数据交换平台下载电子保单时传输编码为：107003
                if ("108001".equals(transactionCode)) {
                    head.addElement("partnerCode");
                    head.addElement("messageId").addText(seqNo);//流水号
                    head.addElement("transactionCode").addText("108005");// 意外险标示 108005:代表徐州学平意外险
                    head.addElement("transactionEffectiveDate").addText(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                    head.addElement("user");
                    head.addElement("password");
                    Element body = info.addElement("body");
                    Element epolicyInfoReprint = body.addElement("epolicyInfoReprint");
                    epolicyInfoReprint.addElement("policyNo").addText(t.getPolicyNo());
                    epolicyInfoReprint.addElement("messageFlag");
                    epolicyInfoReprint.addElement("emailFlag");
                    epolicyInfoReprint.addElement("returnPDFFlag");
                } else if ("107001".equals(transactionCode)) {
                    head.addElement("partnerCode");
                    head.addElement("transactionCode").addText("107003");// 意外险标示 107003:代表徐州监护人责任险
                    head.addElement("messageId").addText(seqNo);//流水号
                    head.addElement("transactionEffectiveDate").addText(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                    head.addElement("user");
                    head.addElement("password");
                    Element body = info.addElement("body");
                    Element entity = body.addElement("entity");
                    entity.addElement("plcNo").addText(t.getPolicyNo());
                    entity.addElement("elcMsgFlag");
                    entity.addElement("elcMobile");
                    entity.addElement("elcEmlFlag");
                    entity.addElement("elcEmail");
                    entity.addElement("returnPDF");
                }

                issuePolicyLogEntity.setParam(info.asXML());
                issuePolicyLogEntity.setTimes(0);
                issuePolicyLogEntity.setIfCode(t.getIfCode());
                issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
                // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
                t.setSendStatus("2");
                issueRecordEntityMapper.updateByPrimaryKeySelective(t);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理统一格式的电子保单
     *
     * @param t
     */
    private void handleEpolicy(IssueRecordEntity t, IssueEnum issueEnum) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费:从响应体中解析出 url，去调用数据中心的接口，下载电子保单
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";//未处理
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());

        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");//电子保单

        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        String resPacket = t.getResPacket();
        log.info("检查mq消费记录的{}请求体是{}", issueEnum.getDesc(), resPacket);

        try {
            Document doc = DocumentHelper.parseText(resPacket);
            Element rootElement = doc.getRootElement();
            Element element = rootElement.element("MAIN");
            // 排除无返回结果的报文
            if (element != null) {
                String policyUrl = element.element("EURL").getStringValue();
                // 拼装下载报文
                Document edoc = DocumentHelper.createDocument();
                Element info = edoc.addElement("body");
                info.addElement("url").addText(policyUrl);

                issuePolicyLogEntity.setParam(info.asXML().replaceAll("&amp;", "&"));
                issuePolicyLogEntity.setTimes(0);
                issuePolicyLogEntity.setIfCode(t.getIfCode());
                issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
                // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
                t.setSendStatus("2");
                issueRecordEntityMapper.updateByPrimaryKeySelective(t);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 食品险（太平）
     *
     * @param t
     */
    private void handleSPXTP(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费:从响应体中解析出 url，去调用数据中心的接口，下载电子保单
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";//未处理
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());

        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");//电子保单

        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        String resPacket = t.getResPacket();
        log.info("检查mq消费记录的食品险（太平）请求体是" + resPacket);

        try {
            Document doc = DocumentHelper.parseText(resPacket);
            Element rootElement = doc.getRootElement();
            Element element = rootElement.element("Body");
            // 排除无返回结果的报文
            if (element != null) {
                String policyUrl = element.element("PolicyUrl").getStringValue();
                // 拼装下载报文
                Document epolicyDoc = DocumentHelper.createDocument();
                Element info = epolicyDoc.addElement("body");
                info.addElement("url").addText(policyUrl);

                issuePolicyLogEntity.setParam(info.asXML());
                issuePolicyLogEntity.setTimes(0);
                issuePolicyLogEntity.setIfCode(t.getIfCode());
                issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
                // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
                t.setSendStatus("2");
                issueRecordEntityMapper.updateByPrimaryKeySelective(t);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 实责校责（大地）
     *
     * @param t
     */
    private void handleSZXZDD(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费  ： 需要组织报文，去调用接口
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());
        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");
        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());

        String url = "";
        try {

            Document doc = DocumentHelper.parseText(t.getResPacket());
            Element rootElement = doc.getRootElement();

            List<Element> respChildElements = rootElement.element("Body").elements();
            for (Element child : respChildElements) {

                String policyNo = child.element("PolicyNo").getStringValue();
                if (t.getPolicyNo().equals(policyNo)) {
                    url = child.element("PolicyUrl").getStringValue();
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        // 组装报文
        Map<String, String> map = new HashMap<>();
        map.put("ApplicationFormCode", t.getApplicationFormCode());
        map.put("url", url);

        String param = JsonUtils.toJson(map);
        issuePolicyLogEntity.setParam(param);
        issuePolicyLogEntity.setMainType(t.getMainType());
        issuePolicyLogEntity.setTimes(0);
        issuePolicyLogEntity.setIfCode(t.getIfCode());
        issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
        // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
        t.setSendStatus("2");
        issueRecordEntityMapper.updateByPrimaryKeySelective(t);

    }

    /**
     * 实责校责（太平/中华联合）
     *
     * @param t
     */
    private void handleSZXZ(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费  ： 需要组织报文，去调用接口
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());
        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");
        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());

        String url = "";
        try {

            Document doc = DocumentHelper.parseText(t.getResPacket());
            Element rootElement = doc.getRootElement();

            List<Element> respChildElements = rootElement.element("Body").elements();
            for (Element child : respChildElements) {
                if (t.getIfCode().equals(ZH_SZ_XZ_POLICY.getIfCode())) {
                    String policyNo = child.element("PolicyCode").getStringValue();
                    if (t.getPolicyNo().equals(policyNo)) {
                        url = child.element("PolicyUrl").getStringValue();
                    }
                } else {
                    String policyNo = child.element("PolicyNo").getStringValue();
                    if (t.getPolicyNo().equals(policyNo)) {
                        url = child.element("PolicyUrl").getStringValue();
                    }
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //添加url处理
        url = url.replaceAll("&amp;", "&");
        // 组装报文
        Document doc = DocumentHelper.createDocument();
        Element info = doc.addElement("body");
        info.addElement("url").addText(url);

        issuePolicyLogEntity.setParam(info.asXML());
        issuePolicyLogEntity.setMainType(t.getMainType());
        issuePolicyLogEntity.setTimes(0);
        issuePolicyLogEntity.setIfCode(t.getIfCode());
        issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
        // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
        t.setSendStatus("2");
        issueRecordEntityMapper.updateByPrimaryKeySelective(t);

    }


    /**
     * 【实责 校责 阳光】
     *
     * @param t
     */
    private void handleXSZAndXZ(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费  ： 需要组织报文，去调用接口
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());
        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");
        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        // 组装报文
        Document doc = DocumentHelper.createDocument();
        Element info = doc.addElement("INSURENCEINFO").addElement("POLICYNO");
        //info.addElement("POLICYNO").addText(policyNo);
        //Element info = doc.addElement("POLICYNO");
        info.addText(t.getPolicyNo());
        issuePolicyLogEntity.setParam(info.asXML());
        issuePolicyLogEntity.setMainType(t.getMainType());
        issuePolicyLogEntity.setTimes(0);
        issuePolicyLogEntity.setIfCode(t.getIfCode());
        issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
        // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
        t.setSendStatus("2");
        issueRecordEntityMapper.updateByPrimaryKeySelective(t);

    }

    /**
     * 广东校责、食品、教职工、校车
     *
     * @param t
     */
    private void handleGD(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费:从响应体中解析出 url，去调用数据中心的接口，下载电子保单
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";//未处理
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());

        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");//电子保单

        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        String resPacket = t.getResPacket();
        log.info("检查mq消费记录的广东校责、食品、教职工、校车请求体是" + resPacket);

        try {
            Document doc = DocumentHelper.parseText(resPacket);
            Element rootElement = doc.getRootElement();
            Element element = rootElement.element("body");
            // 排除无返回结果的报文
            if (element != null) {
                String policyNo = element.element("entity").element("plcNo").getStringValue();
                // 拼装下载报文
                Document info = DocumentHelper.createDocument();
                Element message = info.addElement("Message");
                message.addElement("PolicyNo").addText(policyNo);

                issuePolicyLogEntity.setParam(info.asXML());
                issuePolicyLogEntity.setTimes(0);
                issuePolicyLogEntity.setIfCode(t.getIfCode());
                issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
                // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
                t.setSendStatus("2");
                issueRecordEntityMapper.updateByPrimaryKeySelective(t);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 新济宁学平（线下批量录单）
     *
     * @param t
     */
    private void handleJiNingXPX(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费:从响应体中解析出 url，去调用数据中心的接口，下载电子保单
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";//未处理
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());

        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");//电子保单

        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        String resPacket = t.getResPacket();
        log.info("检查mq消费记录的济宁学平请求体是" + resPacket);

        try {
            Document doc = DocumentHelper.parseText(resPacket);
            Element rootElement = doc.getRootElement();
            Element element = rootElement.element("policyInfoReturns");
            // 排除无返回结果的报文
            if (element != null) {
                String elecUrl = element.element("policyInfoReturn").element("Netaddress").getStringValue();
                // 拼装下载报文
                Document info = DocumentHelper.createDocument();
                Element message = info.addElement("message");
                message.addElement("url").addText(elecUrl);

                issuePolicyLogEntity.setParam(info.asXML());
                issuePolicyLogEntity.setTimes(0);
                issuePolicyLogEntity.setIfCode(t.getIfCode());
                issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
                // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
                t.setSendStatus("2");
                issueRecordEntityMapper.updateByPrimaryKeySelective(t);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 新济宁学平（线上）
     *
     * @param t
     */
    private void handleJiNingXPXOnline(IssueRecordEntity t) {

        String currentTime = DateUtils.getCurrentTime();
        // 消费:从响应体中解析出 url，去调用数据中心的接口，下载电子保单
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";//未处理
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());

        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");//电子保单

        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        String resPacket = t.getResPacket();
        log.info("检查mq消费记录的济宁学平（线上）请求体是" + resPacket);

        try {
            Document doc = DocumentHelper.parseText(resPacket);
            Element rootElement = doc.getRootElement();
            Element element = rootElement.element("policyInfoReturns");
            // 排除无返回结果的报文
            if (element != null) {
                String elecUrl = element.element("policyInfoReturn").element("Netaddress").getStringValue();
                // 拼装下载报文
                Document info = DocumentHelper.createDocument();
                Element message = info.addElement("message");
                message.addElement("url").addText(elecUrl);

                issuePolicyLogEntity.setParam(info.asXML());
                issuePolicyLogEntity.setTimes(0);
                issuePolicyLogEntity.setIfCode(t.getIfCode());
                issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
                // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
                t.setSendStatus("2");
                issueRecordEntityMapper.updateByPrimaryKeySelective(t);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 启东学平险
     *
     * @param t
     */
    private void handleQiDong(IssueRecordEntity t) {

        String findCode = "handleQiDong_" + System.currentTimeMillis();

        String currentTime = DateUtils.getCurrentTime();
        // 消费:从响应体中解析出 url，去调用数据中心的接口，下载电子保单
        IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
        String consumeStatus = "1";//未处理
        issuePolicyLogEntity.setApplicationFormCode(t.getApplicationFormCode());

        issuePolicyLogEntity.setPolicyNo(t.getPolicyNo());
        issuePolicyLogEntity.setType("1");//电子保单

        issuePolicyLogEntity.setStatus(consumeStatus);
        issuePolicyLogEntity.setCreateTime(currentTime);
        issuePolicyLogEntity.setUpdateTime(currentTime);
        issuePolicyLogEntity.setProCode(t.getProCode());
        String resPacket = t.getResPacket();
        log.info("{} 检查mq消费记录的济宁学平（线上）请求体是 {}", findCode, resPacket);

        try {
            Document doc = DocumentHelper.parseText(resPacket);
            Element rootElement = doc.getRootElement();
            Element element = rootElement.element("body");
            // 排除无返回结果的报文
            if (element != null) {
                String elecUrl = element.element("policyInfo").element("policyInfo").element("prdUrl").getStringValue();
                // 拼装下载报文
                Document info = DocumentHelper.createDocument();
                Element message = info.addElement("message");
                message.addElement("url").addText(elecUrl);

                issuePolicyLogEntity.setParam(info.asXML());
                issuePolicyLogEntity.setTimes(0);
                issuePolicyLogEntity.setIfCode(t.getIfCode());
                issuePolicyLogEntityMapper.insert(issuePolicyLogEntity);
                // 只要消息进入消费记录的库，就认为消费成功，修改消息记录表状态,具体有没有成功 记录在消费记录表，有定时任务。
                t.setSendStatus("2");
                issueRecordEntityMapper.updateByPrimaryKeySelective(t);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

}
